<?php

namespace BI\Controller;
use BI\Controller\CommonController;

class IndexController extends CommonController{
    /**
     * @数据分析首页
     */
    public function Index(){
        $params = I('get.');

        $token = $params['token'];
        $confstr = C('crypt_key');
        $mcrypt = new \Org\Util\Mcrypt();

        $chektoken = $mcrypt::decode($token,$confstr);
        $userinfo = explode('+',$chektoken);
        $account = $userinfo[0];
        $accountid = $userinfo[1];
        $checksql = 'select * from spa_admin WHERE account="'.$account.'" and id='.$accountid;
        //验证是后台的管理员
        $checkuser = M()->query($checksql);
        if(empty($checkuser))
            $this->redirect('http://www.s2.com/admin',[],3,'非法操作,登录重试');

        //验证是否有报表的权限
        $reporturl = '/admin/newreport/index';
        $admingroup = 'select group_id from spa_auth_group_access WHERE uid='.$accountid;
        $group = M()->query($admingroup);
        $groupid = $group[0]['group_id'];
        $authgrouprule = 'select * from spa_auth_group WHERE status=1 AND id='.$groupid;
        $grouprule = M()->query($authgrouprule);
        $rules = $grouprule[0]['rules'];
        $reportrulesql = 'select id from spa_auth_rule WHERE `name`= "'.$reporturl.'" and status=1';
        $reportrule = M()->query($reportrulesql);
        $reportruleid = $reportrule[0]['id'];
        $rulesarr = explode(',',$rules);
        if(!in_array($reportruleid,$rulesarr)){
//            $this->error('非法操作');
            $this->redirect('http://www.s2.com/admin',[],3,'非法操作,登录重试');
        }

        $code = empty($params['code']) ? 'Spaceplus' : $params['code'];
        $this->assign('code',$code);

        $AnalyzeData=new BdataConfigController($code,1);
		
        //查询条件
        $getWhere=$AnalyzeData->fetchAllConditions();
        //保存模型时可选条件项
        $getCondition=$AnalyzeData->getConditionColumn();
	    $this->assign('Condition',$getCondition);

        $this->assign('where',$getWhere);  
        $this->assign('item',$AnalyzeData->getAxisConfigDetail());
        $this->assign('iscount',$AnalyzeData->getCountAxisConfig());
        
        $list = M('eggList')->field('id,title')->select();
        $this->assign('listtype',$list);
        
        $this->assign('uom',M()->query('select id,title,uomgroup,ismain from mt_uom'));
        $this->assign('count',M()->query('select id,code,title from mt_count'));        

        $this->display();
    }
    
    /*
     * 提取关联关系
     * return =array('main'=>'区域，组团，开发商，楼盘名称','secondary'=>'业态，细分业态')
     */
    public function getrelation(){
        
        $data=F('Bdata/relation');
        $obj=M();
        if(empty($data)){
            $data['main']=$obj->query('select b.id as qid,qy,c.id as zid,zt,d.id as kid,a.kfs,e.id as pid,a.lpmc from mt_gx as a left join mt_region as b on a.qy=b.title left join mt_plate as  c on a.zt=c.title left join mt_company as d on a.kfs=d.title left join mt_project as e on a.lpmc=e.title');
           // $data['secondary']=
            
            $formats=$obj->query('select fid,b.title as ftitle,sfid,c.title as stitle from mt_formats_subformats as a inner join mt_formats as b on a.fid=b.id inner join mt_sub_formats as c on c.id=a.sfid');
            foreach($formats as $val){
                if(!isset($data['secondary'][$val['fid']])){
                    $data['secondary'][$val['fid']]=array(
                        'id'=>$val['fid'],
                        'name'=>$val['ftitle']
                    );
                }
                $data['secondary'][$val['fid']]['item'][]=array(
                    'id'=>$val['sfid'],
                    'name'=>$val['stitle']
                ); 
            }
            F('Bdata/relation',$data);
        }

        echo json_encode($data);
    } 
}
